<template>
  <div class="app-container">
    <NormalForm pageTitle="登录日志" :operateForm="queryForm" :formList="formList" :enumOpt="enumOpt"
      @handleQuery="handleQuery" @handleReset="handleReset" />
    <div class="g-card" style="padding: 0 20px 20px 20px">
      <div class="right-btn" style="display: flex; justify-content: space-between; margin-right: 0;">
        <div>
          <el-button type="info" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['system:config:export']">导出</el-button>
        </div>
        <div>
          <el-button icon="el-icon-delete" :disabled="currentSelectList.length == 0" @click="handleDelete" v-hasPermi="['system:config:remove']">删除</el-button>
          <el-button type="info" plain icon="el-icon-delete" @click="handleClean" v-hasPermi="['monitor:operlog:remove']">清空</el-button>
        </div>
      </div>
      <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="访问编号" align="center" prop="infoId" />
        <el-table-column label="用户名称" align="center" prop="userName" min-width="140" />
        <el-table-column label="登录地址" align="center" prop="ipaddr" width="130" :show-overflow-tooltip="true" />
        <el-table-column label="登录地点" align="center" prop="loginLocation" :show-overflow-tooltip="true" />
        <el-table-column label="浏览器" align="center" prop="browser" />
        <el-table-column label="操作系统" align="center" prop="os" />
        <el-table-column label="登录状态" align="center" prop="status">
          <template slot-scope="scope">
            <span>{{ loginStatusOpt[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作信息" align="center" prop="msg" min-width="180" />
        <el-table-column label="登录日期" align="center" prop="loginTime" min-width="140">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.loginTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
    </div>
  </div>
</template>

<script>
import { list, delLogininfor, cleanLogininfor } from "@/api/monitor/logininfor";
import { downLoadFile } from "@/utils/common";

export default {
  name: "Logininfor",
  data() {
    return {
      queryForm: {
        ipaddr: null,
        userName: null,
        status: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '登录地址',
          value: '',
          prop: 'ipaddr',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '用户名称',
          value: '',
          prop: 'userName',
          type: 'INPUT',
          emunName: ''
        },
        {
          label: '登录状态',
          value: '',
          prop: 'status',
          type: 'SELECT',
          emunName: 'loginStatusList'
        },
        {
          label: '登录',
          value: null,
          prop: 'dateRange',
          type: 'DATE_RANGE',
          emunName: '',
          formate: 'yyyy-MM-dd'
        },
      ],
      enumOpt: {
        loginStatusList: [],
      },
      loginStatusOpt: {},
      loading: true,
      currentSelectList: [],
      total: 0,
      tableList: [],
    };
  },
  created() {
    this.handleQuery();
    this.getDicts("sys_common_status").then((res) => {
      if (res.code == 200) {
        this.enumOpt.loginStatusList = res.data;
        res.data.forEach(item => {
          this.loginStatusOpt[item.dictValue] = item.dictLabel;
        })
      }
    });
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getTableListFn();
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryForm = {
        ipaddr: null,
        userName: null,
        status: null,
        dateRange: [],
        pageNum: 1,
        pageSize: 20,
      },
      this.getTableListFn();
    },
    handleSizeChange(val) {
      this.queryForm.pageNum = 1
      this.queryForm.pageSize = val
      this.getTableListFn()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val
      this.getTableListFn()
    },
    /** 查询登录日志列表 */
    getTableListFn() {
      this.loading = true;
      const params = {
        ipaddr: this.queryForm.ipaddr,
        userName: this.queryForm.userName,
        status: this.queryForm.status,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
        pageNum: this.queryForm.pageNum,
        pageSize:  this.queryForm.pageSize,
      }
      list(params).then((res) => {
        this.tableList = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.currentSelectList = selection.map((item) => item.infoId);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.infoId || this.currentSelectList;
      this.$confirm('是否确认删除访问编号为"' + infoIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return delLogininfor(infoIds);
      }).then(() => {
        this.getTableListFn();
        this.$message.success("删除成功");
      })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm("是否确认清空所有登录日志数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return cleanLogininfor();
      }).then(() => {
        this.getTableListFn();
        this.$message.success("清空成功");
      }).catch(function () { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const params = {
        ipaddr: this.queryForm.ipaddr,
        userName: this.queryForm.userName,
        status: this.queryForm.status,
        beginTime: this.queryForm.dateRange?.[0],
        endTime: this.queryForm.dateRange?.[1],
      }
      downLoadFile('/system/logininfor/export', params, '登录日志.xlsx')
    },
  },
};
</script>

<style lang="scss" scoped>
@import "../../../styles/base.scss";
</style>
